<template>
  <div id="login">
    <div class="modal-mask">
      <div class="modal-mask-wrapper">
        <div class="modal-mask-container">
          <div class="main"></div>
          <div class="form">
            <h3 @click="showRegister">创建账户</h3>
            <transition name="slide">
              <div :class="{ show: isShowRegister }" class="register">
                <input
                  type="text"
                  v-model="register.username"
                  placeholder="用户名"
                />
                <input
                  type="password"
                  v-model="register.password"
                  placeholder="密码"
                />
                <p :class="{ error: register.isError }">
                  {{ register.notice }}
                </p>
                <p v-show="!register.isError">
                  创建账号后，请记住用户名和密码
                </p>
                <div class="button" @click="onRegister">创建账号</div>
              </div>
            </transition>
            <h3 @click="showLogin">登录</h3>
            <transition name="slide">
              <div :class="{ show: isShowLogin }" class="login">
                <input
                  type="text"
                  v-model="login.username"
                  placeholder="请输入用户名"
                />
                <input
                  type="password"
                  v-model="login.password"
                  @keyup.enter="onLogin"
                  placeholder="请输入密码"
                />
                <p :class="{ error: login.isError }">{{ login.notice }}</p>
                <p v-show="!login.isError">
                  请输入用户名和密码
                </p>
                <div class="button" @click="onLogin">
                  登录
                </div>
              </div>
            </transition>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
export default {
  data() {
    return {
      isShowRegister: false,
      isShowLogin: true,
      login: {
        username: '',
        password: '',
        notice: '',
        isError: false,
      },
      register: {
        username: '',
        password: '',
        notice: '',
        isError: false,
      },
    }
  },
  methods: {
    ...mapActions({
      loginUser: 'login',
      registerUser: 'register',
    }),
    showRegister() {
      this.isShowRegister = true
      this.isShowLogin = false
    },
    showLogin() {
      this.isShowRegister = false
      this.isShowLogin = true
    },
    onRegister() {
      let result1 = this.validUsername(this.register.username)
      if (!result1.isValid) {
        this.register.isError = true
        this.register.notice = result1.notice
        return
      }
      let result2 = this.validPassword(this.register.password)
      if (!result2.isValid) {
        this.register.isError = true
        this.register.notice = result2.notice
        return
      }

      this.registerUser({
        username: this.register.username,
        password: this.register.password,
      })
        .then(() => {
          this.register.isError = false
          this.register.notice = ''
          this.$router.push({ path: 'notebooks' })
        })
        .catch((err) => {
          this.register.isError = true
          this.register.notice = err.msg
        })
    },
    onLogin() {
      let result1 = this.validUsername(this.login.username)
      if (!result1.isValid) {
        this.login.isError = true
        this.login.notice = result1.notice
        return
      }
      let result2 = this.validPassword(this.login.password)
      if (!result2.isValid) {
        this.login.isError = true
        this.login.notice = result2.notice
        return
      }
      this.login.isError = false
      this.login.notice = ''
      this.loginUser({
        username: this.login.username,
        password: this.login.password,
      })
        .then(() => {
          this.login.isError = false
          this.login.notice = ''
          this.$router.push({ path: 'notebooks' })
        })
        .catch((err) => {
          this.login.isError = true
          this.login.notice = err.msg
        })
    },
    validUsername(username) {
      return {
        isValid: /^[\w\u4e00-\u9fa5]{3,15}$/.test(username),
        notice: '用户名必须是3~15个字符，仅限于字母、数字、下划线和中文',
      }
    },
    validPassword(password) {
      return {
        isValid: /^.{6,16}$/.test(password),
        notice: '密码长度为6~16个字符',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.modal-mask {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: table;
  transition: opacity 0.3s ease;

  &-wrapper {
    display: table-cell;
    vertical-align: middle;
  }

  &-container {
    width: 800px;
    height: 500px;
    margin: 0px auto;
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
    transition: all 0.3s ease;
    font-family: Helvetica, Arial, sans-serif;
    display: flex;
    .main {
      flex: 1;
      background: #36bc64
        url(//cloud.hunger-valley.com/17-12-13/38476998.jpg-middle) center
        center no-repeat;
    }
    .form {
      width: 270px;
      border-left: 1px solid #ccc;
      overflow: hidden;
      h3 {
        padding: 10px 20px;
        font-weight: normal;
        font-size: 16px;
        border-top: 1px solid #eee;
        cursor: pointer;
        text-align: left;
        &:nth-of-type(2) {
          border-bottom: 1px solid #eee;
        }
      }

      .button {
        background-color: #2bb964;
        height: 36px;
        line-height: 36px;
        text-align: center;
        font-weight: bold;
        color: #fff;
        border-radius: 4px;
        margin-top: 18px;
        cursor: pointer;
      }
      .login,
      .register {
        border-top: 0;
      }
      .login,
      .register {
        padding: 0px 20px;

        height: 0;
        overflow: hidden;
        transition: height 0.4s;

        &.show {
          height: 193px;
        }
        input {
          display: block;
          width: 100%;
          height: 35px;
          line-height: 35px;
          padding: 0 6px;
          border-radius: 4px;
          border: 1px solid #ccc;
          outline: none;
          font-size: 14px;
          margin-top: 10px;
        }
        input:focus {
          border: 1px solid #9dcaf8;
        }

        p {
          font-size: 12px;
          margin-top: 10px;
          color: #444;
          text-align: left;
        }
        .error {
          color: red;
        }
      }
      .register {
        &.show {
          border-top: 1px solid #eee;
        }
      }
    }
  }
}
</style>
